﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleParalell
{
    class Program
    {
        private static int number = 0;
        static void Main(string[] args)
        {
            //DoWork();
            Stopwatch stop = new Stopwatch();
            stop.Start();
            //Parallel.For(0, 100000, t =>
            //{
            //    Test();
            //});
            //stop.Stop();
            //Console.WriteLine(stop.ElapsedMilliseconds);  //与上面方法为同步
            //stop.Start();
            for (int i = 0; i < 100000; i++)
            {
                Task.Run(() =>
                {
                    Test();
                });
            }
            stop.Stop();
            Console.WriteLine(stop.ElapsedMilliseconds);   //与上面方法为异步
            ////速度太慢不如Task
            //for (int i = 0; i < 100000; i++)
            //{
            //    Thread thread = new Thread(Test);
            //    thread.Start();
            //}
            Console.ReadLine();
        }
        
       
        static async Task DoWork()
        {
            //Parallel.For(0, 10, t =>
            //{
            //    Console.WriteLine("Start" + "(" + t + ")" + "task id:" + Task.CurrentId + " Thread id:" + Thread.CurrentThread.ManagedThreadId);
            //    //Task.Delay(500);
            //    Console.WriteLine("End" + "(" + t + ")" + "task id:" + Task.CurrentId + " Thread id:" + Thread.CurrentThread.ManagedThreadId);

            //});

            Parallel.For(0, 50, t =>
            {
                Test();
            });

            //Task.Run(() => Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId));
            //Task.Run(() => Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId));
            //Task.Run(() => Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId));
            //Task.Run(() => Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId));
            //Task.Run(() => Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId));
            //Task.Run(() => Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId));
            //Task.Run(() => Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId));
            //Thread thread1 = new Thread(new ThreadStart(() => Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId)));
            //Thread thread2 = new Thread(new ThreadStart(() => Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId)));
            //Thread thread3 = new Thread(new ThreadStart(() => Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId)));
            //Thread thread4 = new Thread(new ThreadStart(() => Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId)));
            //Thread thread5 = new Thread(new ThreadStart(() => Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId)));
            //Thread thread6 = new Thread(new ThreadStart(() => Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId)));
            //Thread thread7 = new Thread(new ThreadStart(() => Console.WriteLine("ThreadId:" + Thread.CurrentThread.ManagedThreadId)));

            //thread1.Start();
            //thread2.Start();
            //thread3.Start();
            //thread4.Start();
            //thread5.Start();
            //thread6.Start();
            //thread7.Start();
            //Thread.Sleep(1000);
            Console.WriteLine("complete");
        }

        static void Test()
        {
            if (number % 2 == 0)
            {
                //Thread.Sleep(100);
                if (number % 2 != 0)
                {
                    Console.WriteLine($"numberError" + number);
                }
                Console.WriteLine($"numberDouble"+number);
            }
            else
            {
                if (number % 2 == 0)
                {
                    Console.WriteLine($"numberError" + number);
                }
                Console.WriteLine($"numberOdd" + number);
            }
            number++;
        }
    }
}
